<template>
  <el-select v-model="innervalue" placeholder="请选择"
             @change="_dicvalue=>{
   changselect(_dicvalue)}"
  >
    <el-option
        v-for="item in dataList"
        :key="`${item.value}`"
        :label="`${item.label}`"
        :value="`${item.value}`">
    </el-option>
  </el-select>
</template>
<script>
import axios from 'axios';
import { find } from 'lodash-es';

export default {
  data() {
    return {
      innervalue: `${this.$props.modelValue||''}`,
      dataList: [],
    };
  },
  props: {
    modelValue: {type: [String,Number]},
    code: { type: [String] },
  },
  watch: {
    '$props.modelValue'(_value) {
      this.innervalue=`${_value||''}`
    },
    '$props.code': async function (_value) {
      this.initdatalist(_value);
    },
  },
  mounted() {

    this.initdatalist(this.$props.code);
  },

  methods: {
    async changselect(_dicvalue) {
      var dicone = find(this.dataList, item => item.value == _dicvalue);
      this.$emit('update:modelValue', dicone?.value);
      this.$emit('change', dicone);
    },
    async initdatalist(code) {
      var oneresult = await axios.post(`/api/tb_dict/apis?_name=getbycode`, {
        code: code,
      }).then(res => res.data);
      var dic = oneresult && oneresult.data;
      if (!dic?.id) {
        this.dataList = [];
        return [];
      }
      var dicdetaillist = await axios.post(`/api/tb_dictdetails/apis?_name=getlist`, {
        dic_id: dic?.id,
      }).then(res => res.data);
      var dic_list = dicdetaillist && dicdetaillist.data || [];
      this.dataList = dic_list;
    },
  },
};
</script>
